package com.jdaw.datastructure.queue;

public class CircleArrayQueue {
    public static void main(String[] args) {
        CircleArray circleArray=new CircleArray(4);
        circleArray.addQueue(5);
        circleArray.addQueue(4);
        circleArray.addQueue(6);
        circleArray.showQueue();
    }
}

class CircleArray{
    private int maxSize;
    private int front;
    private int rear;//默认设为0
    private int[] array;//默认设为0

    //初始化的构造器
    public CircleArray(int maxSize){
        this.maxSize=maxSize;
        array=new int[maxSize];
    }

    //判断队列是否满
    public boolean isFull(){
        return (rear+1)%maxSize==front;
    }

    //判断队列是否为空
    public boolean isEmpty(){
        return rear==front;
    }

    //添加数据到队列
    public boolean addQueue(int n){
        if(!isFull()){
            array[rear]=n;
            rear=(rear+1)%maxSize;
            return true;
        }
        return false;
    }

    //获取队列数据，出队列
    public int getQueue(){
        if(!isEmpty()){
            int value=array[front];
            front=(front+1)%maxSize;
            return value;
        }
        throw new RuntimeException("队列为空不可取");
    }

    //展示有效数据的个数
    public int size(){
        return (rear+maxSize-front)%maxSize;
    }

    //展示队列所有数据
    public void showQueue(){
        if(!isEmpty()){
            for(int i=front;i<front+size();i++){
                System.out.println(array[i%maxSize]);
            }
        }
    }

    //显示头元素
    public int headQueue(){
        if(!isEmpty()){
            return array[front];
        }
        throw  new RuntimeException("没有头元素");
    }
}
